{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "google",
   "metadata": {},
   "source": [
    "##### Copyright 2025 Google LLC."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "apache",
   "metadata": {},
   "source": [
    "Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "you may not use this file except in compliance with the License.\n",
    "You may obtain a copy of the License at\n",
    "\n",
    "    http://www.apache.org/licenses/LICENSE-2.0\n",
    "\n",
    "Unless required by applicable law or agreed to in writing, software\n",
    "distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "See the License for the specific language governing permissions and\n",
    "limitations under the License.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "basename",
   "metadata": {},
   "source": [
    "# organize_day"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "link",
   "metadata": {},
   "source": [
    "<table align=\"left\">\n",
    "<td>\n",
    "<a href=\"https://colab.research.google.com/github/google/or-tools/blob/main/examples/notebook/contrib/organize_day.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
    "</td>\n",
    "<td>\n",
    "<a href=\"https://github.com/google/or-tools/blob/main/examples/contrib/organize_day.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/github_32px.png\"/>View source on GitHub</a>\n",
    "</td>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "doc",
   "metadata": {},
   "source": [
    "First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "install",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install ortools"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "description",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "  Organizing a day in Google CP Solver.\n",
    "\n",
    "  Simple scheduling problem.\n",
    "\n",
    "  Problem formulation from ECLiPSe:\n",
    "  Slides on (Finite Domain) Constraint Logic Programming, page 38f\n",
    "  http://eclipse-clp.org/reports/eclipse.ppt\n",
    "\n",
    "\n",
    "  Compare with the following models:\n",
    "  * MiniZinc: http://www.hakank.org/minizinc/organize_day.mzn\n",
    "  * Comet: http://www.hakank.org/comet/organize_day.co\n",
    "  * Gecode: http://hakank.org/gecode/organize_day.cpp\n",
    "\n",
    "  This model was created by Hakan Kjellerstrand (hakank@gmail.com)\n",
    "  Also see my other Google CP Solver models:\n",
    "  http://www.hakank.org/google_or_tools/\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "code",
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "from ortools.constraint_solver import pywrapcp\n",
    "\n",
    "#\n",
    "# No overlapping of tasks s1 and s2\n",
    "#\n",
    "\n",
    "\n",
    "def no_overlap(solver, s1, d1, s2, d2):\n",
    "  b1 = solver.IsLessOrEqualVar(s1 + d1, s2)  # s1 + d1 <= s2\n",
    "  b2 = solver.IsLessOrEqualVar(s2 + d2, s1)  # s2 + d2 <= s1\n",
    "  solver.Add(b1 + b2 >= 1)\n",
    "\n",
    "\n",
    "def main():\n",
    "\n",
    "  # Create the solver.\n",
    "  solver = pywrapcp.Solver('Organizing a day')\n",
    "\n",
    "  #\n",
    "  # data\n",
    "  #\n",
    "  n = 4\n",
    "\n",
    "  tasks = list(range(n))\n",
    "  work, mail, shop, bank = tasks\n",
    "  durations = [4, 1, 2, 1]\n",
    "\n",
    "  # task [i,0] must be finished before task [i,1]\n",
    "  before_tasks = [[bank, shop], [mail, work]]\n",
    "\n",
    "  # the valid times of the day\n",
    "  begin = 9\n",
    "  end = 17\n",
    "\n",
    "  #\n",
    "  # declare variables\n",
    "  #\n",
    "  begins = [solver.IntVar(begin, end, 'begins[%i]% % i') for i in tasks]\n",
    "  ends = [solver.IntVar(begin, end, 'ends[%i]% % i') for i in tasks]\n",
    "\n",
    "  #\n",
    "  # constraints\n",
    "  #\n",
    "  for i in tasks:\n",
    "    solver.Add(ends[i] == begins[i] + durations[i])\n",
    "\n",
    "  for i in tasks:\n",
    "    for j in tasks:\n",
    "      if i < j:\n",
    "        no_overlap(solver, begins[i], durations[i], begins[j], durations[j])\n",
    "\n",
    "  # specific constraints\n",
    "  for (before, after) in before_tasks:\n",
    "    solver.Add(ends[before] <= begins[after])\n",
    "\n",
    "  solver.Add(begins[work] >= 11)\n",
    "\n",
    "  #\n",
    "  # solution and search\n",
    "  #\n",
    "  db = solver.Phase(begins + ends, solver.INT_VAR_DEFAULT,\n",
    "                    solver.INT_VALUE_DEFAULT)\n",
    "\n",
    "  solver.NewSearch(db)\n",
    "\n",
    "  num_solutions = 0\n",
    "  while solver.NextSolution():\n",
    "    num_solutions += 1\n",
    "    print('begins:', [begins[i].Value() for i in tasks])\n",
    "    print('ends:', [ends[i].Value() for i in tasks])\n",
    "    print()\n",
    "\n",
    "  print('num_solutions:', num_solutions)\n",
    "  print('failures:', solver.Failures())\n",
    "  print('branches:', solver.Branches())\n",
    "  print('WallTime:', solver.WallTime(), 'ms')\n",
    "\n",
    "\n",
    "main()\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
